<style>
.header-font {
  font-size: 15px;
  font-weight: bold;
}
.line {
  margin: 10px 0;
  width: 100%;
  border: 0.5px solid rgb(218, 218, 218);
}
.table {
  margin-top: 10px;
}
.paging {
  float: right;
  margin-top: 10px;
}
</style>
<template>
  <div>
    <div class="header-font">
      <Icon type="md-apps"/>
      <span>参数配置</span>
    </div>
    <div class="line"></div>
    <!-- <Button
      icon="md-add"
      type="primary"
      @click="handleClick">
      数据配置
    </Button>-->
    <parameter-config-drawer @config-valid="handleEdit"></parameter-config-drawer>
    <Row>
      <Col span="2">
        <Upload
          ref="upload"
          :show-upload-list="false"
          :on-success="handleSuccess"
          :format="['xls','xlsx']"
          :max-size="500"
          :on-format-error="handleFormatError"
          :on-exceeded-size="handleMaxSize"
          :headers="{
            'agent': 'Web',
            'token': token,
          }"
          :data="{'name':'import'}"
          action="/api/data/customer/in"
        >
          <Button icon="ios-cloud-upload-outline" :loading="uploadLoading">导入预约用户</Button>
        </Upload>
      </Col>
      <Col span="2">
        <Upload
          ref="upload"
          :show-upload-list="false"
          :on-success="handleSuccess"
          :format="['xls','xlsx']"
          :max-size="500"
          :on-format-error="handleFormatError"
          :on-exceeded-size="handleMaxSize"
          :headers="{
            'agent': 'Web',
            'token': token,
          }"
          :data="{'name':'import'}"
          action="/api/data/user/in"
        >
          <Button icon="ios-cloud-upload-outline" :loading="uploadLoading">导入注册用户</Button>
        </Upload>
      </Col>
      <Col span="2">
        <a href="http://static.baisha.work/xls/customer.xls">预约用户模板</a>
      </Col>
      <Col span="2">
        <a href="http://static.baisha.work/xls/register.xls">注册用户模板</a>
      </Col>
    </Row>

    <Table class="table" :columns="columns" :data="configList" :loading="loading" border></Table>
    <!-- <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>-->
  </div>
</template>
<script>
import { mapActions, mapState } from 'vuex'
import ParameterConfigDrawer from '_c/parameter-config-drawer'
import TablePage from '_c/table-page'
import { getToken } from '../../../libs/util'
export default {
  components: {
    ParameterConfigDrawer,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '系统管理参数配置',
      index: '',
      actionUrl1: '/api/data/customer/in',
      actionUrl2: '/api/data/user/in',
      confirmData: {},
      uploadLoading: false,
      loading: true,
      token: getToken(),
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '参数名称',
          key: 'des',
          minWidth: 200,
          align: 'center'
        },
        {
          title: '参数',
          key: 'value',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            if (params.row.key === 'launchImg') {
              return h('div', [
                h('img', {
                  attrs: {
                    src: params.row.host + params.row.value,
                    style: 'width: 170px; height: 90px;'
                  },
                  style: {
                    marginTop: '5px'
                  }
                })
              ])
            } else {
              return h('span', params.row.value ? params.row.value : '-')
            }
            // else if (params.row.id === 8 && params.row.value) {
            //   return h('div', [
            //     h('a', {
            //       attrs: {
            //         href: `http://${params.row.value}`,
            //         target: '_blank',
            //         style: 'width: 170px; height: 90px;'
            //       },
            //       style: {
            //         marginTop: '5px'
            //       }
            //     }, params.row.value)
            //   ])
            // }
          }
        },
        {
          title: '操作',
          key: 'cz',
          minWidth: 120,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small',
                    ghost: 'ghost'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.handleClick(params.row.id, params.row)
                    }
                  }
                },
                '修改'
              )
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      configList: state => state.parameterConfig.configList,
      dataCount: state => state.parameterConfig.configDataCount,
      historyData: state => state.parameterConfig.configHistoryData,
      pageSize: state => state.parameterConfig.configPageSize
    })
  },
  methods: {
    ...mapActions([
      'getConfigList',
      'editConfigData',
      'getConfigCurrentRow',
      'updateCustomerDrawerState',
      'changeConfigPage',
      'changeConfigPageSize'
    ]),
    handleClick (id, row) {
      // console.log(row)
      let newRow = {}
      this.id = id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getConfigCurrentRow(newRow)
      this.updateCustomerDrawerState(true)
    },
    async handleEdit (playload) {
      this.loading = true
      // console.log(playload)
      const resp = await this.editConfigData(playload)
      if (resp.data.state === '000') {
        this.$Message.success('修改成功')
        this.getConfigList({}).then(res => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateCustomerDrawerState(false)
    },
    changePage (index) {
      this.page = index
      this.changeConfigPage(index)
    },
    changePageSize (value) {
      this.rows = value
      this.changeConfigPageSize(value)
      this.changePage(this.page)
    },
    // handleBeforeUpload (file) {
    //   const fileExt = file.name.split('.').pop().toLocaleLowerCase()
    //   if (fileExt === 'xlsx' || fileExt === 'xls') {
    //     this.file = file
    //   } else {
    //     this.$Notice.warning({
    //       title: '文件类型错误',
    //       desc: '文件：' + file.name + '不是EXCEL文件，请选择后缀为.xlsx或者.xls的EXCEL文件。'
    //     })
    //   }
    //   return false
    // },
    handleFormatError (file) {
      this.$Notice.warning({
        title: '文件格式不正确',
        desc: '请上传jpg、jpeg、png格式的图片'
      })
    },
    handleMaxSize (file) {
      this.$Notice.warning({
        title: '文件大小不正确',
        desc: '请上传500kb以下大小的图片'
      })
    },
    handleSuccess () {
      this.$Message.success('上传成功！')
    }
  },
  created () {
    this.getConfigList({}).then(res => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
  }
}
</script>
